package com.dgz.csmall.product.controller;

import com.dgz.csmall.product.pojo.param.BrandAddNewParam;
import com.dgz.csmall.product.pojo.param.BrandUpdateInfoParam;
import com.dgz.csmall.product.pojo.vo.BrandListItemVO;
import com.dgz.csmall.commons.pojo.vo.PageData;
import com.dgz.csmall.product.service.BrandService;
import com.dgz.csmall.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Author duguozheng
 * @Date 2023/05/11  23:31
 * @Description 处理品牌相关请求的控制器类
 * @Version 1.0
 */
@RestController
@RequestMapping("/brands/")
@Api(tags = "02. 品牌管理模块")
@Slf4j
@Validated
public class BrandController {

    @Autowired
    BrandService brandService;

    public BrandController() {
        log.info("创建控制器对象：BrandController");
    }

    //http://localhost:9180/brands/add-new
    @PostMapping("add-new")
    @ApiOperation("新增品牌")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid BrandAddNewParam brandAddNewParam) {
        log.debug("开始处理【新增品牌】的请求，参数：{}", brandAddNewParam);
        brandService.addNew(brandAddNewParam);
        log.debug("处理【新增品牌】的请求，完成！");
        return JsonResult.ok();
    }

    @PostMapping("delete")
    @ApiOperation("删除品牌")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "品牌ID", required = true, dataType = "long")
    public JsonResult delete(@Range(min = 1, message = "删除品牌失败！请提交合法的ID值！")
                                 @RequestParam Long id) {
        log.debug("开始处理【删除品牌】任务，参数：{}", id);
        brandService.delete(id);
        log.debug("处理【删除品牌】任务完成！");
        return JsonResult.ok();
    }
    @PostMapping("updateInfo")
    @ApiOperation("修改品牌")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "品牌ID", required = true, dataType = "long")
    public JsonResult updateInfo(@Range(min = 1, message = "删除品牌失败！ID必须为正整数！")
                                 @RequestParam long id, @Valid BrandUpdateInfoParam brandUpdateInfoParam){
        log.debug("开始处理【修改品牌】任务，参数：{},{}",id,brandUpdateInfoParam);
        brandService.updateInfoById(id,brandUpdateInfoParam);
        log.debug("处理【修改品牌】任务完成！");
        return JsonResult.ok();
    }

    // http://localhost:9180/album/list
    @GetMapping("list")
    @ApiOperation("查询品牌列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "页码", paramType = "query")
    })
    public JsonResult list(@Range(min = 1, message = "查询品牌列表失败，请提供正确的页码值！") Integer page) {
        log.debug("开始处理【查询品牌列表】的请求，页码:{}", page);
        if (page == null || page < 1) {
            page = 1;
        }
        PageData<BrandListItemVO> pageData = brandService.list(page);
        return JsonResult.ok(pageData);
    }

}
